﻿using System;
using System.Collections.Generic;

namespace Schuifpuzzel.Model
{
    /// <summary>
    /// This class holds a List of highScores
    /// </summary>


    [Serializable()]
    public class HighScoreList
    {
        private List<Score> highScores;

        public List<Score> HighScores
        {
            get { return highScores; }
        }

        public HighScoreList()
        {
            highScores = new List<Score>();
        }

        /// <summary>
        /// Adds a score to the list and then sorts the list.
        /// </summary>
        /// <param name="score">The score to be added</param>

        public void AddScore(Score score)
        {
            if (score.Name == null) throw new ArgumentException("Score must contain a name to be added to the list!");
            if (score.Time == 0) throw new ArgumentException("Score must contain a time to be added to the list!");
            if (score.Moves == 0) throw new ArgumentException("Score must contain the amount of moves to be added to the list!");

            highScores.Add(score);
            highScores.Sort();
        }

        

        /// <summary>
        /// Check if the given time and moves results in a high score.
        /// If not, it returns -1
        /// If so, it returns the position that the high score would receive
        /// </summary>
        /// <param name="time"></param>
        /// <param name="moves"></param>
        /// <returns></returns>

        public int CheckIfIsHighScore(int time, int moves)
        {
            for(int i = 0; i < highScores.Count && i < 5;i++)
            {
                if (time == highScores[i].Time)
                {
                    if (moves < highScores[i].Moves)
                    {
                        return i + 1;
                    }
                }
                else if (time < highScores[i].Time)
                {
                    return i + 1;
                }
            }

            return -1;
        }
    }
}
